package jp.tkym.labs.proc;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Map;

import jp.tkym.labs.proc.PreparedMessageProcesser.PreparedMessage;

class PreparedJavaProcess{
	private ObjectOutputStream notifyWriter;
	private PreparedMessageProcesser messageProcesser = new PreparedMessageProcesser();
	private JavaProcessWorker worker;
	
	PreparedJavaProcess(JavaProcessWorker worker) throws IOException{
		this.notifyWriter = new ObjectOutputStream(worker.getProcess().getOutputStream());
		this.worker = worker;
	}
	
	void sendMessage(PreparedMessage message) throws IOException {
		notifyWriter.writeObject(message);
		notifyWriter.close();
	}
	
	<T extends Runnable> void precall(String classRunnable) throws IOException{
		sendMessage(messageProcesser.createTask(classRunnable));
	}
	
	JavaProcessWorker restart(String classHasMain, Map<String, String> prop) throws Exception{
		sendMessage(messageProcesser.createMain(classHasMain, prop));
		return worker;
	}
	
	void exit() throws Exception{
		sendMessage(messageProcesser.createExit());
		worker.submit().get();
	}
}